home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE23 / EXPRESS / SOURCE / EXPEDITR.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-02-24  |  10.2 KB  |  355 lines

  1. unit ExpEditr;
  2.  
  3. interface
  4.  
  5. uses SysUtils, WinTypes, WinProcs, Classes, Graphics, Forms, Controls,
  6.   Dialogs, Buttons, StdCtrls, ExtCtrls, Grids, DsgnIntf, ExpComp;
  7.  
  8. type
  9.   { TNamedExpressionsProperty definition }
  10.  
  11.   TNamedExpressionsProperty = class(TPropertyEditor)
  12.   public
  13.     function   GetAttributes: TPropertyAttributes; override;
  14.     function   GetValue: string; override;
  15.     procedure  Edit; override;
  16.   end;
  17.  
  18.   { TfrmExpressionEditor definition }
  19.  
  20.   TfrmExpressionEditor = class(TForm)
  21.     StrGrdNamedExpressions: TStringGrid;
  22.     bvlBevel: TBevel;
  23.     hdrNamedExpressions: THeader;
  24.     btnOK: TBitBtn;
  25.     btnCancel: TBitBtn;
  26.     btnSave: TButton;
  27.     btnLoad: TButton;
  28.     OpenDialog1: TOpenDialog;
  29.     SaveDialog1: TSaveDialog;
  30.     edtSource: TEdit;
  31.     btnClear: TButton;
  32.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  33.     procedure FormActivate(Sender: TObject);
  34.     procedure btnLoadClick(Sender: TObject);
  35.     procedure btnSaveClick(Sender: TObject);
  36.     procedure btnClearClick(Sender: TObject);
  37.     procedure StrGrdNamedExpressionsSelectCell(Sender: TObject; Col,
  38.       Row: Longint; var CanSelect: Boolean);
  39.   private
  40.     { Private declarations }
  41.     FRow: Integer;
  42.   public
  43.     { Public declarations }
  44.     function  Execute: Boolean;
  45.     procedure LoadFromFile(FileToLoad: string);
  46.     procedure AddNamedExpression(ExpName, Expression: string);
  47.     function  GetRowCount: Integer;
  48.     function  GetExpressionNameAt(Idx: Integer): string;
  49.     function  GetExpressionAt(Idx: Integer): string;
  50.   end;
  51.  
  52. procedure Register;
  53.  
  54. var
  55.   NamedExpStringList: TStringList;
  56.  
  57. implementation
  58.  
  59. {$R *.DFM}
  60.  
  61. procedure Register;
  62. begin
  63.   RegisterPropertyEditor(TypeInfo(TStrings),
  64.                          TExpression,
  65.                          'NamedExpressions',
  66.                          TNamedExpressionsProperty);
  67. end;
  68.  
  69. {  TNamedExpressionsProperty declarations  }
  70.  
  71. function TNamedExpressionsProperty.GetAttributes: TPropertyAttributes;
  72. begin
  73.   Result := [paDialog];
  74. end;
  75.  
  76. function TNamedExpressionsProperty.GetValue: string;
  77. begin
  78.   Result := Format('(%s)', [GetPropType^.Name]);
  79. end;
  80.  
  81. procedure TNamedExpressionsProperty.Edit;
  82. var
  83.   frmExpressionEditor: TfrmExpressionEditor;
  84.   ExpCount: Integer;
  85.   Name, Expression: string;
  86. begin
  87.   NamedExpStringList := TStringList(TExpression(GetComponent(0)).NamedExpressions);
  88.  
  89.   frmExpressionEditor := TfrmExpressionEditor.Create(Application);
  90.  
  91.   try
  92.     if (NamedExpStringList.Count = 1) then
  93.         begin
  94.           frmExpressionEditor.LoadFromFile(NamedExpStringList[0]);
  95.           frmExpressionEditor.edtSource.Text := ExpandFileName(NamedExpStringList[0]);
  96.         end
  97.     else
  98.         if (NamedExpStringList.Count > 0) then
  99.             begin
  100.               if (NamedExpStringList.Count / 2) > 19 then
  101.                   frmExpressionEditor.strgrdNamedExpressions.RowCount :=  Round(NamedExpStringList.Count / 2) + 10;
  102.  
  103.               with NamedExpStringList do
  104.                  for ExpCount := 0 to Count - 1 do
  105.                      if Odd(ExpCount + 1) then
  106.                         Name := Strings[ExpCount]
  107.                      else
  108.                         begin
  109.                           Expression := Strings[ExpCount];
  110.                           frmExpressionEditor.AddNamedExpression(Name, Expression);
  111.                         end;
  112.             end;
  113.  
  114.     if frmExpressionEditor.Execute then
  115.        if frmExpressionEditor.ModalResult = mrOK then
  116.           begin
  117.             NamedExpStringList.Clear;
  118.  
  119.             if frmExpressionEditor.edtSource.Text <> '' then
  120.                NamedExpStringList.Add(ExtractFileName(frmExpressionEditor.edtSource.Text))
  121.             else
  122.                if (frmExpressionEditor.GetRowCount > 0) then
  123.                    for ExpCount := 0 to frmExpressionEditor.GetRowCount - 1 do
  124.                       begin
  125.                         NamedExpStringList.Add(UpperCase(frmExpressionEditor.GetExpressionNameAt(ExpCount)));
  126.                         NamedExpStringList.Add(frmExpressionEditor.GetExpressionAt(ExpCount));
  127.                       end;
  128.           end;
  129.   finally
  130.     frmExpressionEditor.Free;
  131.   end;
  132. end;
  133.  
  134. {  TfrmExpressionEditor declarations  }
  135.  
  136. function TfrmExpressionEditor.Execute: Boolean;
  137. begin
  138.   Result := False;
  139.  
  140.   if (Self <> nil) then
  141.       begin
  142.         ShowModal;
  143.  
  144.         if (ModalResult = mrOk) then
  145.             Result := True;
  146.       end;
  147. end;
  148.  
  149. procedure TfrmExpressionEditor.LoadFromFile(FileToLoad: string);
  150. var
  151.   TempStringList: TStringList;
  152.   ExpCount: Integer;
  153.   Name: string;
  154.   Exp: string;
  155. begin
  156.   TempStringList := TStringList.Create;
  157.  
  158.   try
  159.     strgrdNamedExpressions.Enabled := False;
  160.  
  161.     FRow := 0;
  162.  
  163.     TempStringList.LoadFromFile(FileToLoad);
  164.  
  165.     if (TempStringList.Count > 19) then
  166.         strgrdNamedExpressions.RowCount := TempStringList.Count + 10;
  167.  
  168.     for ExpCount := 0 to TempStringList.Count - 1 do
  169.        begin
  170.          Name := Copy(TempStringList[ExpCount], 1, Pos('=', TempStringList[ExpCount]) - 1);
  171.          Exp  := Copy(TempStringList[ExpCount], Pos('=', TempStringList[ExpCount]) + 1, 255);
  172.  
  173.          AddNamedExpression(Name, Exp);
  174.        end;
  175.   finally
  176.     strgrdNamedExpressions.Enabled := True;
  177.     TempStringList.Free;
  178.   end;
  179. end;
  180.  
  181. procedure TfrmExpressionEditor.AddNamedExpression(ExpName, Expression: string);
  182. begin
  183.   with strgrdNamedExpressions do
  184.     begin
  185.       Cells[0, FRow] := ExpName;
  186.       Cells[1, FRow] := Expression;
  187.     end;
  188.  
  189.   Inc(FRow);
  190. end;
  191.  
  192. function TfrmExpressionEditor.GetRowCount: Integer;
  193. begin
  194.   Result := 0;
  195.  
  196.   repeat
  197.     with strgrdNamedExpressions do
  198.        if (Cells[0, Result] = '') and
  199.           (Cells[1, Result] = '') then
  200.            break;
  201.  
  202.     if (strgrdNamedExpressions.Cells[0, Result] = '') then
  203.         begin
  204.           with strgrdNamedExpressions do
  205.             begin
  206.               Col := 0;
  207.               Row := Result;
  208.               SetFocus;
  209.             end;
  210.  
  211.           raise EStringListError.Create('Expression at row ' +
  212.                                         IntToStr(Result + 1) +
  213.                                         ' does not have a Name');
  214.         end;
  215.  
  216.     if (strgrdNamedExpressions.Cells[1, Result] = '') then
  217.         begin
  218.           with strgrdNamedExpressions do
  219.             begin
  220.               Col := 1;
  221.               Row := Result;
  222.               SetFocus;
  223.             end;
  224.  
  225.           raise EStringListError.Create('Expression is missing at row ' +
  226.                                         IntToStr(Result + 1));
  227.         end;
  228.  
  229.     Inc(Result);
  230.   until (Result = strgrdNamedExpressions.RowCount);
  231. end;
  232.  
  233. function TfrmExpressionEditor.GetExpressionNameAt(Idx: Integer): string;
  234. begin
  235.   Result := strgrdNamedExpressions.Cells[0, Idx];
  236. end;
  237.  
  238. function TfrmExpressionEditor.GetExpressionAt(Idx: Integer): string;
  239. begin
  240.   Result := strgrdNamedExpressions.Cells[1, Idx];
  241. end;
  242.  
  243. procedure TfrmExpressionEditor.FormCloseQuery(Sender: TObject;
  244.   var CanClose: Boolean);
  245. var
  246.   Idx: Integer;
  247.   tmpExpression: TExpression;
  248. begin
  249.   case ModalResult of
  250.      mrCancel: CanClose := True;
  251.  
  252.      mrOK: begin
  253.              if (GetRowCount > 0) then
  254.                  begin
  255.                    tmpExpression := TExpression.Create(Self);
  256.  
  257.                    try
  258.                      tmpExpression.SilentExceptions := True;
  259.  
  260.                      for Idx := 0 to GetRowCount - 1 do
  261.                         begin
  262.                           tmpExpression.Expression := GetExpressionAt(Idx);
  263.  
  264.                           if (not tmpExpression.ValidExpression) then
  265.                               begin
  266.                                 with strgrdNamedExpressions do
  267.                                    begin
  268.                                      Col := 1;
  269.                                      Row := Idx;
  270.                                      SetFocus;
  271.                                    end;
  272.  
  273.                                 raise TExpressionException.Create('Expression at Row ' +
  274.                                                                   IntToStr(Idx + 1) +
  275.                                                                   ' is not a valid expression');
  276.                               end;
  277.                         end;
  278.                    finally
  279.                      tmpExpression.Free;
  280.                    end;
  281.                  end;
  282.            end;
  283.      else
  284.            CanClose := False;
  285.   end;
  286. end;
  287.  
  288. procedure TfrmExpressionEditor.FormActivate(Sender: TObject);
  289. begin
  290.   FRow := 0;
  291. end;
  292.  
  293. procedure TfrmExpressionEditor.btnLoadClick(Sender: TObject);
  294. var
  295.   TempStringList: TStringList;
  296. begin
  297.   btnSave.Font.Style := [];
  298.   TButton(Sender).Font.Style := [fsBold];
  299.  
  300.   OpenDialog1.InitialDir := ExtractFilePath(edtSource.Text);
  301.   OpenDialog1.FileName   := ExtractFileName(edtSource.Text);
  302.  
  303.   if OpenDialog1.Execute then
  304.      begin
  305.        edtSource.Text := OpenDialog1.FileName;
  306.        LoadFromFile(OpenDialog1.FileName);
  307.      end;
  308. end;
  309.  
  310. procedure TfrmExpressionEditor.btnSaveClick(Sender: TObject);
  311. var
  312.   TempStringList: TStringList;
  313.   ExpCount: Integer;
  314. begin
  315.   btnLoad.Font.Style := [];
  316.   TButton(Sender).Font.Style := [fsBold];
  317.  
  318.   SaveDialog1.InitialDir := ExtractFilePath(edtSource.Text);
  319.   SaveDialog1.FileName   := ExtractFileName(edtSource.Text);
  320.  
  321.   if SaveDialog1.Execute then
  322.      if (GetRowCount > 0) then
  323.          begin
  324.            edtSource.Text := SaveDialog1.FileName;
  325.  
  326.            TempStringList := TStringList.Create;
  327.  
  328.            try
  329.              for ExpCount := 0 to GetRowCount - 1 do
  330.                  TempStringList.Add(GetExpressionNameAt(ExpCount) +
  331.                                     '=' +
  332.                                     GetExpressionAt(ExpCount));
  333.  
  334.              TempStringList.SaveToFile(SaveDialog1.FileName);
  335.            finally
  336.              TempStringList.Free;
  337.            end;
  338.          end;
  339. end;
  340.  
  341. procedure TfrmExpressionEditor.btnClearClick(Sender: TObject);
  342. begin
  343.   edtSource.Text := '';
  344. end;
  345.  
  346. procedure TfrmExpressionEditor.StrGrdNamedExpressionsSelectCell(
  347.   Sender: TObject; Col, Row: Longint; var CanSelect: Boolean);
  348. begin
  349.   with strgrdNamedExpressions do
  350.     if (Row = RowCount - 1) AND ((Cells[0, Row] <> '') OR (Cells[1, Row] <> '')) then
  351.         RowCount := RowCount + 10;
  352. end;
  353.  
  354. end.
  355.